var app = getApp();
var util = require('../../utils/util.js');
Page({
  data: {
    //送养列表
    giveList: [],
    giveList1: [],
    giveList2: [],
    giveTime: '',
    giveCity: ''
  },
  onLoad: function(options) {
    //找到系统状态位置
    this.setData({
      statusBarHeight: app.globalData.systemInfo.statusBarHeight + 15
    })
    var location = app.globalData.location;
    if (location == null) {
      this.setData({
        giveCity: '全部地区'
      })
    } else {
      this.setData({
        giveCity: location.city
      })
    }
    this.getGivePetListByCity();
  },
  onReachBottom: function() {
    var giveList = this.data.giveList;
    //拿到最后一个送养的时间
    this.data.giveTime = giveList[giveList.length - 1].createDate;
    this.getGivePetListByCity();
  },
  onPullDownRefresh: function() {
    this.data.refresh = true;
    this.data.giveTime = '';
    this.getGivePetListByCity();
  },

  //获取送养列表
  getGivePetListByCity: function() {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'getGivePetListByCity',
      method: 'POST',
      data: {
        city: that.data.giveCity,
        time: that.data.giveTime
      },
      success: function(r) {
        if (r.data.code == 200) {
          var data = r.data.data;
          //左边数组
          var arr1 = [];
          //右边数组
          var arr2 = [];
          for (var i = 0; i < data.length; i++) {
            //找到图片高度
            data[i].imageHeight = util.getImageHeight(data[i].height, data[i].width, 0.48);
            data[i].talkType = 0;

            //如果是双数则放到第一个盒子
            if (i % 2 == 0) {
              arr1.push(data[i]);
            } else {
              arr2.push(data[i]);
            }
          }
          //如果是下拉则清空
          if (that.data.refresh) {
            that.setData({
              giveList: data,
              giveList1: arr1,
              giveList2: arr2,
              refresh: false
            })
            wx.stopPullDownRefresh();
          } else {
            if (data.length == 0) {} else {
              that.setData({
                giveList: that.data.giveList.concat(data),
                giveList1: that.data.giveList1.concat(arr1),
                giveList2: that.data.giveList2.concat(arr2)
              })
            }
          }
        } else {
          wx.showToast({
            title: '获取送养信息失败',
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络断开连接,获取送养信息失败',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },
  //返回下一个页面
  rollback: function() {
    wx.navigateBack({
      delta: 1
    })
  }, //跳转切换地区
  selLocation: function() {
    wx.navigateTo({
      url: '/pages/selLocation/selLocation',
    })
  },
  //跳转详情
  showTalk:function(e){
    var id = e.detail.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/giveInfo/giveInfo?id='+id,
    })
  }
})